import { request } from '@/utils/request'
import type {
  SystemSettingsResponse,
  GeneralSettingsUpdate,
  SecuritySettingsUpdate,
  NotificationSettingsUpdate,
  SystemSettingsUpdate,
  BackupSettingsUpdate,
  EmailTestRequest,
  BackupRecordList
} from '@/types/settings'

// 系统设置相关接口

// 获取系统设置
export const getSystemSettings = () => {
  return request.get<SystemSettingsResponse>('/settings')
}

// 更新基本设置
export const updateGeneralSettings = (data: GeneralSettingsUpdate) => {
  return request.put('/settings/general', data)
}

// 更新安全设置
export const updateSecuritySettings = (data: SecuritySettingsUpdate) => {
  return request.put('/settings/security', data)
}

// 更新通知设置
export const updateNotificationSettings = (data: NotificationSettingsUpdate) => {
  return request.put('/settings/notification', data)
}

// 更新系统配置
export const updateSystemSettings = (data: SystemSettingsUpdate) => {
  return request.put('/settings/system', data)
}

// 更新备份设置
export const updateBackupSettings = (data: BackupSettingsUpdate) => {
  return request.put('/settings/backup', data)
}

// 测试邮件发送
export const testEmail = (data: EmailTestRequest) => {
  return request({
    url: '/settings/test-email',
    method: 'POST',
    data
  })
}

// 获取备份历史
export const getBackupHistory = () => {
  return request<BackupRecordList>({
    url: '/settings/backup/history',
    method: 'GET'
  })
}

// 创建备份
export const createBackup = () => {
  return request.post('/settings/backup/create')
}

// 删除备份
export const deleteBackup = (backupId: string) => {
  return request.delete(`/settings/backup/${backupId}`)
}

// 下载备份
export const downloadBackup = (backupId: string) => {
  return request({
    url: `/settings/backup/download/${backupId}`,
    method: 'GET',
    responseType: 'blob'
  })
}